﻿$(document).ready(function() {
    var source;
    JsInit();
});


function JsInit() {
    SetDrag(); //设置拖动层
    SetCustomTip(); //自定义Tip效果
    SetFocusStyle(); //设置活动元素标签
    SetTabStyle(); //设置表格样式
    SetButtonStyle(); //设置按钮样式
    SetCalendar(); //调置日历控件
    SetCheckAll(); //全选
    SetNavTabMenu(); //设置导航菜单
    SetLeftMenu(); //设置左边菜单
    //    SetDateControls(); //设置日期控件
 
}

/*日期选择*/
function SetDateControls() {


    $(".dttxt").datepicker({
        picker: "<IMG class=picker alt=\"\" src=\"/themes/shared/images/s.gif\" align=middle>"
    }).each(function() {
        this.onfocus = null;
    });  ; 
}


function SetLeftMenu() {
    $(".left .menu.openSlide dd:not(dd:has(dl))").mouseover(function() {
        $(this).addClass("over");
    });
    $(".left .menu.openSlide dd:not(dd:has(dl))").mouseout(function() {
        $(this).removeClass("over");
    });
    $(".shrink").click(function() {
        $(".left").toggle();
    });
    $(".left .menu.openSlide dt").click(function() {
        if ($(this.parentNode).hasClass("slideDown")) {
            $(this.parentNode).find("dd").not($.makeArray($(this.parentNode).find("dd dd"))).slideDown("fast", function() {
                var cs = $(this.parentNode).attr("class").replace("slideDown", "")
                $(this.parentNode).attr("class", cs)
            });
        } else {
            $(this.parentNode).find("dd").not($.makeArray($(this.parentNode).find("dd dd"))).slideUp("fast", function() {
                if (!$(this.parentNode).hasClass("slideDown")) {
                    var cs = $(this.parentNode).attr("class") + " slideDown";
                    $(this.parentNode).attr("class", cs)
                }
            });
        }
    }
   );

}



function SetNavTabMenu() {
    //navTabContainer导航菜单，
    //navTabContainer .navTabMenu .nav 为菜单项,菜单项必须设置bind属性，该属性与菜单对应的块的class对应 菜单项class带有action时为活动项
    //navTabContainer tbody.navTab 为菜单对应的块，点击相应的菜单就相示相应块的快。class中的属性与菜单中的bind对应
    //navTabContainer .navTabNext 和 .navTabPerv 为上一步和下一步的按钮


    if ($(".navTabContainer").length > 0) {
        var navTabContainer = $(".navTabContainer");
        navTabContainer.find(".navTabMenu .nav").click(function() {
            var sel = String.format("tbody.{0}", $(this).attr("bind"));
            navTabContainer.find("tbody.navTab").hide();
            navTabContainer.find(sel).show();
            navTabContainer.find(".navTabMenu .nav").removeClass("action");
            $(this).addClass("action");
        });

        navTabContainer.find(".navTabNext").click(function() {
            var next = navTabContainer.find(".navTabMenu .action+.nav");
            if (next.length > 0)
                next[0].click();
        });
        navTabContainer.find(".navTabPerv").click(function() {
            var action = navTabContainer.find(".navTabMenu .action");
            if (action.length > 0) {
                var i = 0;
                navTabContainer.find(".navTabMenu .nav").each(function() {
                    if (this == action[0]) {
                        return false;
                    } else {
                        i++;
                    }
                });
                if (i > 0)
                    i--;
                navTabContainer.find(".navTabMenu .nav")[i].click();
            }

        });
    }
}

//调置日历控件
function SetCalendar() {
    $(".calendar").click(function() {
        calendar(this);
    });
}

//设置拖动层
function SetDrag() {
    $(".drag").easydrag();
    $(".drag .close").click(function() {
        var findDrag = null;
        findDrag = function(obj) {
            if ($(obj).hasClass("drag"))
                return obj;
            else
                return findDrag(obj.parentNode);
        };
        $(findDrag(this)).hide();
    });

    $(".drag").each(function() {
        var divLeft = document.documentElement.clientWidth / 2 - $(this).width() / 2;
        var divTop = document.documentElement.clientHeight / 2 - $(this).height() / 2;
        $(this).css({ "left": divLeft, "top": divTop });

        $(window).scroll(function() {
            var divScrollTop = document.documentElement.scrollTop + divTop;
            var divScrollLeft = divLeft - document.documentElement.scrollLeft / 2;

            $(this).animate({
                top: divScrollTop + "px",
                left: divScrollLeft + "px"
            }, 5);
        });

    });
    //$(".drag").setHandler("dt");

}
//自定义Tip效果
function SetCustomTip() {

    function SetPoint(obj) {
        var l = $(obj).offset().left + "px";
        var t = $(obj).offset().top + $(obj).height() + "px";
        var bindDiv = $("#" + $(obj).attr("bind"));
        //alert(t);
        bindDiv.css({ left: l, top: t });
    }
    $(".customTip").each(function() {
        var bindDiv = $("#" + $(this).attr("bind"));
        //bindDiv.bgiframe(true);
        bindDiv.mousemove(function() {
            var tipId = $(this)[0].id;
            var tipDiv = this;
            tipDiv.move = true;
            var toCallBack = String.format('if ($("#{0}")[0].move) {$("#{0}").fadeIn("slow")}', tipId);
            setTimeout(toCallBack, 200);
        });
        bindDiv.mouseout(function() {
            var tipId = $(this)[0].id;
            var tipDiv = this;
            if (tipDiv.move) {
                tipDiv.move = false;
                var toCallBack = String.format('if (!$("#{0}")[0].move) {$("#{0}").fadeOut("slow")}', tipId);
                setTimeout(toCallBack, 300);
            }
        });
    });

    $(".customTip").mousemove(function() {
        SetPoint(this);
        var tipId = $(this).attr("bind");
        var tipDiv = $("#" + tipId)[0];
        tipDiv.move = true;
        //$(tipDiv).fadeIn("slow");
        var toCallBack = String.format('if ($("#{0}")[0].move) {$("#{0}").fadeIn("slow")}', tipId);
        setTimeout(toCallBack, 200);
    });
    $(".customTip").mouseout(function() {
        SetPoint(this);
        var tipId = $(this).attr("bind");
        var tipDiv = $("#" + tipId)[0];
        if (tipDiv.move) {
            tipDiv.move = false;
            var toCallBack = String.format('if (!$("#{0}")[0].move) {$("#{0}").fadeOut("slow")}', tipId);
            setTimeout(toCallBack, 300);
        }

    });


}
//设置活动对像的样式
function SetFocusStyle() {
    $(":text,textarea").focus(function() {
        $(this).addClass("focus");
        //$(this.parentNode.parentNode).addClass("focus");
    });
    $(":text,textarea").blur(function() {
        $(this).removeClass("focus");
        //$(this.parentNode.parentNode).removeClass("focus");
    });
}

//设置tab的样式-----------------------------------开始
function SetTabStyle() {
    $("table.tab   tr").mouseover(changecolor);
    $("table.tab   tr").mouseout(changeback);
    $("table.tab tr").click(function() {
       
        $(this.parentNode).find("tr").removeClass("rowsel");
        $(this).addClass("rowsel");
    });
}
var highlightcolor = '#eafcd5';
//此处clickcolor只能用win系统颜色代码才能成功,如果用#xxxxxx的代码就不行,还没搞清楚为什么:(
var clickcolor = '#51b2f6';

function changecolor() {
    source = event.srcElement;
    if (source.tagName == "TR" || source.tagName == "TABLE" || source.tagName == "TH")
        return;
    var p = source.parentElement;
    while (source.tagName != "TD") {
        source = source.parentElement;
        if (source.tagName == "TH") return;
    }
    source = source.parentElement;
    cs = source.children;
    if (cs == null) return;
    if (cs.length > 1) {
        if (cs[1].style.backgroundColor != highlightcolor && source.id != "nc" && cs[1].style.backgroundColor != clickcolor)
            for (i = 0; i < cs.length; i++) {
            cs[i].style.backgroundColor = highlightcolor;
        }
    }
}

function changeback() {
    source = event.srcElement; //joe add this line
    if (event.fromElement.contains(event.toElement) || source.contains(event.toElement) || source.id == "nc")
        return
    if (source.tagName == "TH") return;
    if (cs == null) return;
    if (cs.length > 1) {
        if (event.toElement != source && cs[1].style.backgroundColor != clickcolor)
        //source.style.backgroundColor=originalcolor
            for (i = 0; i < cs.length; i++) {
            cs[i].style.backgroundColor = "";
        }
    }
}
//设置tab的样式-----------------------------------结束 
//设置按钮样式
function SetButtonStyle() {
    $(".btnContainer input,input.linkbtn").mouseover(function() {
        $(this).addClass("hover");
    });
    $(".btnContainer input,input.linkbtn").mouseout(function() {
        $(this).removeClass("hover");
    });
}
function GetTruePage(url) {
    var reg = new RegExp("^~/");
    url = url.replace(reg.exec(url), "http://" + location.host + "/");
    return url;
}
//序列化json对像成字符串
function Serialize(obj) {
    switch (obj.constructor) {
        case Object:
            var str = "{";
            for (var o in obj) {
                if (obj[o]!=null) {
                    str += String.format("\"{0}\":{1},", o, Serialize(obj[o]));
                }
                else
                    str += String.format("\"{0}\":{1},", o, "null");
            }
            if (str.substr(str.length - 1) == ",")
                str = str.substr(0, str.length - 1);
            return str + "}";
            break;
        case Array:
            var str = "[";
            for (var o in obj) {
                str += Serialize(obj[o]) + ",";
            }
            if (str.substr(str.length - 1) == ",")
                str = str.substr(0, str.length - 1);
            return str + "]";
            break;
        case Boolean:
            return obj.toString();
            break;
        case Date:
            return "\"" + obj.toString() + "\"";
            break;
        case Function:
            break;
        case Number:
            return obj.toString();
            break;
        case String:
            
            return "\"" + obj.toString() + "\"";
            break;
    }
}
function SetCheckAll() {//表格全选
    $(".tab .cbSel").click(function() {
        var checked = this.checked;
        $(this.parentNode.parentNode.parentNode).find(":checkbox").each(function() { this.checked = checked; });
    });
    $(":checkbox.checkAll").click(function() {
        var checked = this.checked;
        var tag = $($(this).attr("bind"));
        $(tag).find(":checkbox").each(function() { this.checked = checked; });
    });
}

function SetNc(value) {
    var tis = null;
    if (value.title) {
        tis = value.title;
    }
    var spans = $(".navigationContainer span")
    for (var i = 0; i < tis.length; i++) {
        $(spans[i]).html(tis[i]);
    }
}

function doPage(url) {
    window.location = url;
    //window.parent.doPage(url);
}
function GoBack() {
    window.history.back();
}

function goBack() {
    GoBack();
}

function doBack() {
    GoBack();
     
}
